#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cerrno>
#include <unistd.h>
using namespace std;

int main(int argc, char* argv[])
{
    if (argc <= 2)
    {
        cerr << "Usage:" << argv[0] << " <server> <port>" << endl;
        return 1;
    }
    const char* ip = argv[1];
    int port = atoi(argv[2]);

    struct sockaddr_in server_address;
    bzero(&server_address, sizeof(server_address));
    server_address.sin_family = AF_INET;
    inet_pton(AF_INET, ip, &server_address.sin_addr);
    server_address.sin_port = htons(port);
    // 创建套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    assert (sockfd >= 0);

    // 连接服务器
    int ret = connect(sockfd, (struct sockaddr*)&server_address, sizeof(server_address));
    if(ret < 0)
    {
        cerr << "Connection failed: " << strerror(errno) << endl;
    }
    else
    {
        // 连接正常
        const char* oob_data = "Urgent data";
        const char* normal_data = "Normal data";
        // 发送紧急数据
        ret = send(sockfd, oob_data, strlen(oob_data), MSG_OOB);    // flag MSG_OOB 特定标明这次发送的数据
        assert(ret >= 0);
        ret = send(sockfd, oob_data, strlen(oob_data), 0);
        assert (ret >= 0);
        // 发送正常数据
        ret = send(sockfd, normal_data, strlen(normal_data), 0);
        assert(ret >= 0);
    }
    // 关闭套接字
    close(sockfd);

    return 0;
}